import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * 注意：子菜单仅在路由子菜单 children.length >= 1 时出现。Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                  如果设置为true，项目将不会显示在侧边栏中（默认值为false） if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true              如果设置为true，将始终显示根菜单 if set true, will always show the root menu
 *                               如果未设置alwaysShow，则当项目具有多个子路由时， if not set alwaysShow, when item has more than one children route,
 *                               它将变成嵌套模式，否则不显示根菜单 it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect          如果设置为noRedirect，则面包屑中不会重定向 if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'            该名称由<keep-alive>使用（必须设置！！！） the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    控制页面角色（可以设置多个角色） control the page roles (you can set multiple roles)
    title: 'title'               侧边栏和面包屑中显示的名称（推荐集） the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' 图标显示在侧边栏中 the icon show in the sidebar
    noCache: true                如果设置为true，则不会缓存页面（默认值为false） if set true, the page will no be cached(default is false)
    affix: true                  如果设置为true，标记将粘贴在标记视图中 if set true, the tag will affix in the tags-view
    breadcrumb: false            如果设置为false，该项将隐藏在breadcrumb中（默认值为true） if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  如果设置了路径，侧边栏将突出显示您设置的路径 if set path, the sidebar will highlight the path you set
  }
 */

/**
 * 固定路由
 * 没有权限要求的基本页
 * 可以访问所有角色
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '仪表板', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: {
          title: '个人中心',
          icon: 'user',
          noCache: true
        }
      }
    ]
  }
]

/**
 * 异步路由
 * 需要根据用户角色动态加载的路由
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/user',
    component: Layout,
    redirect: '/user/users',
    alwaysShow: true, // 将始终显示根菜单
    name: 'User',
    meta: {
      title: '用户管理',
      icon: 'user',
    },
    children: [
      {
        path: 'users',
        component: () => import('@/views/user/users'),
        name: 'Users',
        meta: {
          icon: 'peoples',
          title: '用户列表',
        }
      },
      {
        path: 'role',
        component: () => import('@/views/user/role'),
        name: 'Role',
        meta: {
          icon: 'lock',
          title: '角色管理',
        }
      }
    ]
  },
  {
    path: '/applets',
    component: Layout,
    redirect: '/applets/banner',
    meta: {
      title: '小程序',
      icon: 'wechat'
    },
    children: [
      {
        path: 'banner',
        component: () => import('@/views/applets/banner'),
        name: 'Applets',
        meta: {
          icon: 'example',
          title: '轮播图',
          affix: false
        }
      },
      {
        path: 'nav',
        component: () => import('@/views/applets/nav'),
        name: 'Nav',
        meta: {
          icon: 'guide',
          title: '导航',
          affix: false
        }
      }
    ]
  },

  // 404页必须放在末尾！！！
  // { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support 需要服务支持
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
